package com.cyjt.operation.ui;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.cyjt.operation.R;
import com.cyjt.operation.adapter.CustomBaseAdapter;
import com.cyjt.operation.adapter.StaticNodesItemAdapter;
import com.cyjt.operation.base.AppContext;
import com.cyjt.operation.base.HandlerMessageCodes;
import com.cyjt.operation.bean.DeviceStatus;
import com.cyjt.operation.bean.StaticNodesItemInfo;
import com.cyjt.operation.view.ActionBarLayout;

public class StaticNodesItemActivity extends Activity {
	private String code;
	private ListView static_node_listview;
	private List<StaticNodesItemInfo> staticNodesItem;
	private StaticNodesItemAdapter staticNodesItemAdapter;
	/** 下拉刷新控件 */
	public static SwipeRefreshLayout mSwipeRefreshLayout;

	private Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HandlerMessageCodes.FETCH_STATIC_NODES_ITEM_LIST_SUCCESS:
				// 获取设备状态成功
				ActionBarLayout.isShowProgressBar(false);
				staticNodesItem = (List<StaticNodesItemInfo>) msg.obj;
				Log.i("demo", "handler");
				initDate(staticNodesItem);
				break;
			case HandlerMessageCodes.FETCH_STATIC_NODES_ITEM_LIST_FAILED:
				break;
			default:
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_staticnode_item_layout);
		code = getIntent().getStringExtra("code");
		initView();
		feachStaticNodesStatus();
	}

	/**
	 * 初始化控件
	 */
	private void initView() {
		ActionBarLayout.setTitle("节点数据");
		ActionBarLayout.isShowProgressBar(true);
		static_node_listview = (ListView) findViewById(R.id.static_node_listview);
		// static_node_listview.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		//
		// }
		// });
		mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiprrefreshLayout_for_static_nodes_status_list);
		mSwipeRefreshLayout.setColorSchemeResources(
				android.R.color.holo_red_dark, android.R.color.holo_green_dark,
				android.R.color.holo_blue_dark, android.R.color.holo_purple);
		mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				ActionBarLayout.isShowProgressBar(true);
				feachStaticNodesStatus();
				mSwipeRefreshLayout.setRefreshing(false);
			}
		});
	}

	/**
	 * 获取节点发送给服务器的数据
	 */
	public void feachStaticNodesStatus() {
		AppContext.getInstance().getVolleyTools()
				.fetchStaticNodesItem(handler, code);
	}

	/**
	 * 加载数据
	 * 
	 * @param staticNodesItem
	 */
	private void initDate(List<StaticNodesItemInfo> staticNodesItem) {
		Log.i("demo", "initDate");
		if (staticNodesItemAdapter == null) {
			staticNodesItemAdapter = new StaticNodesItemAdapter(handler,
					staticNodesItem, StaticNodesItemActivity.this);
			static_node_listview.setAdapter(staticNodesItemAdapter);
		} else {
			staticNodesItemAdapter.notifyDataSetChanged();
		}
	}

}
